#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <stdio.h>
#include <string.h>

using namespace std;

//sudoku
typedef pair<int, int> PII;

int rowHash[10] = {0};
int colHash[10] = {0};
int hash[10]= {0};

bool Valid(vector<vector<char> > &board, int x, int y, int k)
{
    int mask = 1 << k;
    if (rowHash[x] & mask || colHash[y] &mask || hash[(x/3)*3 + y/3] & mask) return false;
    return true;
}

PII findCell(vector<vector<char> > &board)
{
    for (int i = 0; i < 9; i++) {
	for (int j = 0; j < 9; j++) {
	    if (board[i][j] == ' ') return make_pair(i, j);
	}
    }
    return make_pair(-1, -1);
}

bool solveSudokuHelper(vector<vector<char> > &board) {
	    PII point = findCell(board);
	    if (point.first == -1) return true;
	    for (int i = 1; i <= 9; i++) {
		int x = point.first;
		int y = point.second;
		if (!Valid(board, x, y, i)) continue;
		rowHash[x] |= 1 << i;
		colHash[y] |= 1 << i;
		hash[(x/3)*3 + y/3] |= 1 << i;
		board[point.first][point.second] = '0' + i;
		if (solveSudokuHelper(board)) return true;
		board[point.first][point.second] = ' ';
		rowHash[x] &= ~(1 << i);
		colHash[y] &= ~(1 << i);
		hash[(x/3)*3 + y/3] &= ~(1 << i);
	    } 
    return false;
}

    void solveSudoku(vector<vector<char> > &board) {
	for (int i = 0; i < board.size(); i++) 
	    for (int j = 0; j < board.size(); j++) {
		if (board[i][j] != ' ') {
		    int k = board[i][j] - '0';
		    rowHash[i] |= 1 << k;
		    colHash[j] |= 1 << k;
		    hash[i/3*3 + j/3] |= 1 << k;
		}
	    }
	solveSudokuHelper(board);    
    }


vector<char> trans(const string &str)
{
    vector<char> ans;
    for (int i = 0; i < str.size(); i++) {
        ans.push_back(str[i]);
    }
    return ans;
}

//test in 1337 online judge and pass all the test
int main(int argc, char **argv)
{

    cout << "-----------------Test 1--------------------" << endl;
    vector<vector<char> > input(9);
    input[0] = trans("53  7    ");
    input[1] = trans("6  195   ");
    input[2] = trans(" 98    6 ");
    input[3] = trans("8   6   3");
    input[4] = trans("4  8 3  1");
    input[5] = trans("7   2   6");
    input[6] = trans(" 6    28 ");
    input[7] = trans("   419  5");
    input[8] = trans("    8  79");
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++){
            cout << input[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    solveSudoku(input);
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++){
            cout << input[i][j] << " ";
        }
        cout << endl;
    }
    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;

}
